﻿define(['app', 'config', 'services/accountService', 'localization'], function (app, config, anything) {
    
    app.directive("menu", ["$compile", function ($compile) {
        return {
            restrict: "E",
            replace: true,
            scope: {
                data: "="
            },
            template: "<ul><menuitem ng-repeat='item in data' item='item'></menuitem></ul>",
            link: function (scope, element, attr) {
                $compile(element.contents())(scope);
            }
        };
    }])
    .directive("menuitem", ["$compile", "$location", function ($compile, $location) {
        return {
            restrict: "E",
            replace: true,
            scope: {
                item: "="
            },
            template: "<li></li>",
            link: function (scope, element, attr) {
                var item = scope.item;

                if (!item.element) {
                    item.element = element;
                }

                if (item.Route) {
                    
                    var a = angular.element("<a href='/Dashboard/{{item.Route}}' class='menuitem'></a>");
                    element.append(a);

                    if (angular.lowercase($location.path()).indexOf("/dashboard/" + angular.lowercase(item.Route)) == 0) {
                        a.addClass("currentPage");

                        config.currentElement = a;
                    }

                    if (item.Icon !== "") {
                        a.append("<i class='icon glyphicon glyphicon-{{item.Icon}}'></i> ");
                    }

                    a.append("<span t='{{item.Text}}'>{{item.Text}}</span>");
                    a.append("<i class='guide glyphicon glyphicon-chevron-right'></i>");

                    a.on("click", function () {
                        if (config.currentElement && config.currentElement.hasClass("currentPage")) {
                            config.currentElement.removeClass("currentPage");
                        }

                        a.addClass("currentPage");

                        config.currentElement = a;
                    });
                } else {
                    element.append("<span class='text' t='{{item.Text}}'>{{item.Text}}</span>");
                }

                if (angular.isArray(scope.item.Children)) {
                    element.append("<menu data='item.Children' class='submenu'></menu>");
                }

                $compile(element.contents())(scope);
            }
        };
    }]);

    app.controller("MainController", [
        "$scope",
        "$rootScope",
        "$location",
        "AccountService",
        "localize",
        "appEvents",
        "authenticationService",
        function ($scope, $rootScope, $location, AccountService, localize, appEvents, authenticationService) {
            $scope.menuData = config.menu;

            localize.setCurrentModule("Menu");

            localize.refreshSettings(function (availableCultures, currentCulture) {
                $scope.availableCultures = availableCultures;
                $scope.currentCulture = currentCulture;
            });
        
            $scope.selectedLanguageChanged = function () {
                localize.changeCulture($scope.currentCulture);
            };

            $scope.$on("$routeChangeSuccess", function (event, current, previous) {                
                AccountService.refresh().then(function () {
                    var splash = angular.element("#splash");
                    if (splash.is(":visible")) splash.hide();
                });
            
                appEvents.triggerInit();
            });

            $rootScope.signOut = function () {
                authenticationService.signOut().then(function () {
                    window.location = "/";
                });
            };
        }
    ]);

});